import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { listUser } from "@/api/system/user";
const user = {
  state: {
    token: getToken(),
    id: '',
    userId: '',
    name: '',
    avatar: '',
    roles: [],
    permissions: [],
    userList: []
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
    SET_ID: (state, id) => {
      state.id = id
    },
    SET_NAME: (state, name) => {
      state.name = name
    },
    SET_AVATAR: (state, avatar) => {
      state.avatar = avatar
    },
    SET_ROLES: (state, roles) => {
      state.roles = roles
    },
    SET_PERMISSIONS: (state, permissions) => {
      state.permissions = permissions
    },
    SET_USERLIST: (state, userList) => {
      state.userList = userList
    },
    SET_USERID: (state, id) => {
      state.userId = id
    }
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const phone = userInfo.phone
      const password = userInfo.password
      return new Promise((resolve, reject) => {
        console.log('不是吧');
        login(phone, password).then(res => {

          setToken(res.data.token)
          commit('SET_TOKEN', res.data.token)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 获取用户id

    GetUserId(context) {
      return new Promise(((resolve, reject) => {
        getInfo().then(res => {
          context.commit('SET_USERID', res.data.userId)
          console.log(res.data.userId, '这里是多少啊');
          if (res.data.userId === 1 || res.data.userId === 2) {
            console.log('不可能执行啊');
            context.commit('ADD_USER', { root: true })
          }
        })

      }))
    },


    // 获取用户信息
    GetInfo({ commit, state }) {
      return new Promise((resolve, reject) => {
        getInfo().then(res => {
          const user = res.user
          const avatar = (user.avatar == "" || user.avatar == null) ? require("@/assets/images/profile.jpg") : process.env.VUE_APP_BASE_API + user.avatar;
          if (res.roles && res.roles.length > 0) { // 验证返回的roles是否是一个非空数组
            commit('SET_ROLES', res.roles)
            commit('SET_PERMISSIONS', res.permissions)
          } else {
            commit('SET_ROLES', ['ROLE_DEFAULT'])
          }
          // 获取一下用户列表信息
          listUser({
            pageNum: 1,
            pageSize: 1000
          }).then(res => {
            let list = res.rows.map(item => ({
              label: item.nickName,
              value: item.userId
            }))
            commit('SET_USERLIST', list)
          })

          commit('SET_ID', user.userId)
          commit('SET_NAME', user.userName)
          commit('SET_AVATAR', avatar)
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },

    // 退出系统
    LogOut(context) {
      return new Promise((resolve, reject) => {

        context.commit('REST_ROUTER', { root: true })
        context.commit('SET_TOKEN', '')
        context.commit('SET_ROLES', [])
        context.commit('SET_PERMISSIONS', [])
        removeToken()
        resolve()
        // logout(state.token).then(() => {
        //   commit('SET_TOKEN', '')
        //   commit('SET_ROLES', [])
        //   commit('SET_PERMISSIONS', [])
        //   removeToken()
        //   resolve()
        // }).catch(error => {
        //   reject(error)
        // })
      })
    },

    // 前端 登出
    FedLogOut({ commit }) {
      return new Promise(resolve => {
        commit('SET_TOKEN', '')
        removeToken()
        resolve()
      })
    }
  }
}

export default user
